/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.hualu.zks.Dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.JOptionPane;

import com.hualu.zks.Model.User;
import static com.hualu.zks.Dao.ConnDb.conn;

/**
 *
 * @author Administrator
 */
public class UserDao {
    static String username;
    static String password;
    static String user_type="-1";
    static String status;
    private User loginUser = new User();
    //-1 数据库连接失败 0用户名校验失败 1用户名校验成功
    public User checkUserLogin(String username,String password){
        try {
//            this.password = MD5Util.getMD5String(password);
            this.password = password;
            this.username = username;
            Connection conn;
            conn = ConnDb.connDb();
            if(conn==null){
                loginUser.setLoginInfo("-1");
                return loginUser;
            }
            Statement statement = conn.createStatement();
            String sql = "SELECT id, username FROM zks_user WHERE username='"+username+"' AND password='"+this.password+"';";
            try (ResultSet rs = statement.executeQuery(sql)){
                if(rs.next()){
                    loginUser.setId(rs.getInt("id"));
                    loginUser.setUsername(rs.getString("username"));
//                    loginUser.setUserType(rs.getString("memberType"));
//                    loginUser.setRealName(rs.getString("Realname"));
//                    loginUser.setStatus(rs.getString("Status"));
//                    System.err.println(loginUser.getUserType());
//                    String temp = loginUser.getUserType();
//                    if((temp.equals("1")||temp.equals("3"))&&!"0".equals(loginUser.getStatus())){
                        loginUser.setLoginInfo("0");
//                    }else{
//                        System.err.println(loginUser.getUserType());
//                        loginUser.setLoginInfo("-1");
//                    }
                    rs.close();
                    return loginUser;
                }else{
                    rs.close();
                    loginUser.setLoginInfo("-2");
                    return loginUser;
                }
            }
        }    
         catch (SQLException e) {
            System.out.println(Arrays.toString(e.getStackTrace()));
        }
        loginUser.setLoginInfo("-1");
        
        return loginUser;
    }
    
    public static String getUserType(){
        return user_type;
    }
    public static boolean checkOldPwd(String oldPwd){
        return oldPwd.equals(password);
    }
    
    public static boolean editUserPwd(String newPwd) {
        try {
//            Connection conn;
//            conn = ConnDb.connDb();
//            if (conn == null) {
//                return false;
//            }
            Statement statement = conn.createStatement();
            String sql = "UPDATE bd_user SET user_pwd='" + newPwd + "' WHERE user_name='" + username + "';";
            System.out.println("sql_que:" + sql);
            statement.executeUpdate(sql);
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(UserDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
    
    public static boolean adminEditUserPwd(String userId, String newPwd) {
        try {
//            Connection conn;
//            conn = ConnDb.connDb();
//            if (conn == null) {
//                return false;
//            }
            Statement statement = conn.createStatement();
            String sql = "UPDATE bd_user SET user_pwd='" + newPwd + "' WHERE user_id='" + userId + "';";
            System.out.println("sql_que:" + sql);
            statement.executeUpdate(sql);
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(UserDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }

    public static ArrayList<User> getUserList() {
        try {
            ArrayList<User> userList = new ArrayList<>();
//            Connection conn;
//            conn = ConnDb.connDb();
//            if (conn == null) {
//                return null;
//            }
            Statement statement = conn.createStatement();
            String sql = "SELECT user_id,user_name,user_pwd,user_type FROM bd_user;";
            System.out.println("sql_que:"+sql);
            try (ResultSet rs = statement.executeQuery(sql)) {
                while (rs.next()) {
                    User plist = new User();
//                    System.out.println("found user:"+rs.getString("user_name"));
                    plist.setId(rs.getInt("user_id"));
                    plist.setUsername(rs.getString("user_name"));
                    plist.setPassword(rs.getString("user_pwd"));
                    plist.setUserType(rs.getString("user_type"));
                    userList.add(plist);
                }
                rs.close();
                return userList;
            }
        } catch (SQLException ex) {
            Logger.getLogger(UserDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }
    
    public static boolean delUser(String userId) {
        try {
//            Connection conn;
//            conn = ConnDb.connDb();
//            if (conn == null) {
//                return false;
//            }
            Statement statement = conn.createStatement();
            String sql = "DELETE FROM bd_user WHERE user_id = '"+userId+"';";
            System.out.println("sql_que:" + sql);
            statement.executeUpdate(sql);
            return true;
        } catch (SQLException ex) {
            Logger.getLogger(UserDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
    
    public static boolean addUser(String userName,String userPwd) {
        try {
//            Connection conn;
//            conn = ConnDb.connDb();
//            if (conn == null) {
//                return false;
//            }
            Statement statement = conn.createStatement();
            String sql = "SELECT * FROM bd_user WHERE user_name='"+userName+"';";
            System.out.println("sql_que:"+sql);
            try (ResultSet rs = statement.executeQuery(sql)) {
                if (rs.next()) {
                    JOptionPane.showMessageDialog(null, "用户名重复！", "警告", JOptionPane.WARNING_MESSAGE);
                    rs.close();
                    return false;
                } else {
                    rs.close();
                    String sql1 = "INSERT INTO bd_user (user_name,user_pwd,user_type) VALUES ('" + userName + "','" + userPwd + "','0');";
                    System.out.println("sql_que:" + sql1);
                    statement.executeUpdate(sql1);
                    return true;
                }
            }

        } catch (SQLException ex) {
            Logger.getLogger(UserDao.class.getName()).log(Level.SEVERE, null, ex);
        }
        return false;
    }
}
